var mongodb = require('./mongodb');

var Schema = mongodb.mongoose.Schema;  // 模式、策略

var MessageSchema = new Schema({
	msgId: String,
	msgType: String,
	title: String,
	content: String, // 最长1000字节
	target: String, // 目标设备
	receive: { type: Boolean, default: false}, // 调用回执，即消息已接收
	extras:{}, // 直接推送到设备的集合
	createTime: { type: Date, default: Date.now},
	receiveTime: Date,
	isWrap: { type: Boolean, default: false} // 是否完整信息，完整就不需要拉取
});

var Message = mongodb.mongoose.model("Message", MessageSchema); // 模型

var MessageDAO = function(){};

/**
 * Middleware oprate...
 *
	MessageSchema.pre('save', function(next){
		// do stuff
		next();
	});

	MessageSchema.statics = {
		fetch: function(cb) {
			return this.find.sort('xxx')
				exec(cb);
		},
		findById: function(id, cb) {
			return this.findOne({_id: id})
				exec(cb);
		}
	};
 */

/**
 * [save description] 插入一条记录
 * @param  {[type]}   obj      [description] obj 即json对象
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
MessageDAO.prototype.save = function(obj, callback) {
	var instance = new Message(obj);

	instance.save(function(err) {
		callback(err);
	});
};

/**
 * [delete description] 删除一条记录
 * @param  {[type]} conditions [description] conditions 删除的条件 eg:{_id: id}、{ title: 'abc' }
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
MessageDAO.prototype.delete = function(conditions, callback) {
	Message.remove(conditions, function(err, Message) {
		callback(err, Message);
	});
};

/**
 * [save description] 根据id查找对象
 * @param  {[type]}   id      [description] id 即json对象id
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
MessageDAO.prototype.findById = function(id, callback) {
	Message.findById(id, function(err, Message) {
		callback(err, Message);
	});
};

/**
 * [findByName description] 根据某个字段的名称查询
 * @param  {[type]}   cloumName [description]
 * @param  {[type]}   name      [description]
 * @param  {Function} callback  [description]
 * @return {[type]}             [description]
 */
MessageDAO.prototype.findByName = function(cloum_Name, callback) {
	// console.log(arguments);
	Message.findOne(cloum_Name, function(err, obj) {
		// console.log("length = " + arguments.callee.length);
		callback(err, obj);
	});
};

/**
 * [query description] 按条件查询 eg: User.find({age: {$gte: 21, $lte: 65}}, callback);
 * @param  {[type]}   conditions [description] 条件 {age: {$gte: 21, $lte: 65}
 * @param  {Function} callback   [description]
 * @return {[type]}              [description]
 */
MessageDAO.prototype.query = function(conditions, callback) {
	// console.log(arguments);
	Message.find(conditions, function(err, obj) {
		// console.log("length = " + arguments.callee.length);
		callback(err, obj);
	});
};

/**
 * [findOneAndUpdate description] 根据属性查找对象并且更新
 * @param  {[type]}   conditions [description] 匹配属性 { name: 'borne' }
 * @param  {[type]}   update     [description] 更新属性 { name: 'jason borne' }
 * @param  {[type]}   options    [description]
 * @param  {Function} callback   [description]
 * @return {[type]}              [description]
 */
MessageDAO.prototype.findOneAndUpdate = function(conditions, update, callback) {
	Message.findOneAndUpdate(conditions, update, function(err, Message) {
		// console.log(arguments);
		// console.log("length = " + arguments.callee.length);
		callback(err, Message);
	});
};

/**
 * [update description] 更新对象(支持批量)
 * @param  {[type]}   conditions [description] 匹配属性 { name: 'borne' }
 * @param  {[type]}   update     [description] 更新属性 { name: 'jason borne' }
 * @param  {Function} callback   [description]
 * @return {[type]}              [description]
 */
MessageDAO.prototype.update = function(conditions, update, options, callback) {
	// Message.where(conditions).update(update, callback);
	Message.update(conditions, update, options, function(err, numberAffected, Message) {
		// console.log(arguments);
		// console.log("length = " + arguments.callee.length);
		callback(err, numberAffected, Message);
	});
};

module.exports = new MessageDAO();

